package com.tydh.webservice.controller;

import com.alibaba.csb.sdk.HttpCaller;
import com.google.gson.Gson;
import com.tydh.webservice.bean.AllocatScanBean;
import com.tydh.webservice.bean.ParamSettingBean;
import com.tydh.webservice.common.Assert;
import com.tydh.webservice.common.utils.AllSetParam;
import com.tydh.webservice.common.utils.Interfacetype;
import com.tydh.webservice.result.R;
import com.tydh.webservice.result.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 3.5.8	【配发接口】接口
 */
@SuppressWarnings("all")
@CrossOrigin
@RestController
@RequestMapping("/all/allot")
@Slf4j
public class IAllotScanDDService {


    @Autowired
    private AllSetParam allSetParam;

    @Autowired
    private ParamSettingBean paramSettingBean;

    @RequestMapping("/iAllotScanDDService")
    public R scanPackByCsb(@RequestBody AllocatScanBean allocatScanBean)
    {
        Gson gson = new Gson();
        Map<String,String> params = new HashMap<String,String>();

        String baseParams = allSetParam.getBaseParamsByBean(allocatScanBean);
        Map messageHeader = allSetParam.setMessageHeader(gson.toJson(allocatScanBean));

        //messageHeader转为json
        String jsonMessageHeader = gson.toJson(messageHeader);
        params.put("messageHeader", jsonMessageHeader);

        String allocatScanbean = gson.toJson(allocatScanBean);
        params.put("pcsAllotScanReceivePda", allocatScanbean);

        String result;
        try {
            result = HttpCaller.doPost(Interfacetype.URL, Interfacetype.ALLOTSCAN_NAME, paramSettingBean.getVersion(), params, paramSettingBean.getAk(), paramSettingBean.getSk());
            if (result != null) {
                result = result.replaceAll("\\\\","");
            }
            log.info("新一代的数据{}", result);
            Assert.notNull(result, ResultEnum.ALIYUN_RESPONSE_FAIL);
            return  R.ok().data("data", result);
        } catch (Exception ie) {
            log.info(ie.getMessage());
            return R.error().data("error", ie.getMessage());
        }
    }
}
